home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / mouse01g.zip / MOUSE0G.ASM next >
Assembly Source File  |  1992-05-04  |  10KB  |  248 lines

  1.         Title   TSR Microsoft Mouse Driver - Simple Version
  2. ;
  3. ;Author: G.B.Gustafson Feb 1992, gustafson@math.utah.edu.Internet.
  4. ;
  5. ;REFERENCE: Two similar mouse TSR programs exist:
  6. ;
  7. ;    A similar but different TSR is in MENUMOUS.ASM sources from
  8. ;    jmbj@whuts.ATT.COM (BITTMAN). It is located in archive MENUMOUS.ARC;
  9. ;    see below for source directory on Internet.
  10. ;
  11. ;    The MENUMOUS sources break when used with the BUF160_5.ZIP device
  12. ;    driver that expands the keyboard buffer (it does work with the
  13. ;    default keyboard buffer of length 17). The source below for the
  14. ;    keyboard buffer stuffer works with the BUF160_5.ZIP driver.
  15. ;
  16. ;    A shareware product called ARRMOUSE.ZIP is available on Internet
  17. ;    anonymous FTP to wuarchive.wustl.edu in directory ~/mirrors/msdos.
  18. ;    The author is Seth W. Comstock (Version 1.0, March 1990). It has
  19. ;    some good features and should be investigated.
  20. ;
  21. ;USE: The importance of the sources is educational value:
  22. ;
  23. ;     1. A simple, useful TSR that hooks no interrupt vector.
  24. ;     2. A keyboard buffer routine for stuffing keys. It will work
  25. ;        even if the keyboard buffer has been expanded.
  26. ;     3. Mouse driver example. Safe initialization.
  27. ;        See notes below on the event handler.
  28. ;
  29. ;MOUSE SENSITIVITY. A curious feature in mouse code is sensitivity to
  30. ;movement: settling down on a character is subject to much error. In the
  31. ;code below this problem is attacked.
  32. ;
  33. ;    Ideally, the cursor should lock onto a line and not be bumped off
  34. ;    easily as the cursor scans left and right. Further, up and down
  35. ;    motion should lock the cursor onto a column. I should like to scan
  36. ;    up and down columns of numbers with the mouse exactly as I do with
  37. ;    the cursor keys. The code below is an attempt to realize these
  38. ;    features that falls short of perfection.
  39. ;
  40. ;MOUSE DRIVER DEFAULTS. The TSR resets the mouse driver and but sets no
  41. ;defaults for the mouse controls: X,Y sensitivity, doubler.
  42. ;
  43. ;RELEASE TSR. This TSR contains no code to detect its own presence.
  44. ;Furthermore, it contains no code to release it from memory. Code can be
  45. ;written following the ideas of Al Williams, "DOS 5: A Developers Guide",
  46. ;page 516 (M&T Books 1992). See other sources this package.
  47. ;
  48. ;    It is not safe to simply delete this TSR because of the mouse driver
  49. ;    event handler initialization. However, you can issue a mouse driver
  50. ;    reset and then it can be safely deleted by mark/release or resdel.
  51. ;
  52. ;WARRANTY. This source code carries no warranty. Its intended use is
  53. ;information. If you use it, then it was worth writing down: no
  54. ;permission is needed to copy and use this source. I repeat: this is not
  55. ;a software product, and it is serendipity if it happens to be useful.
  56. ;-GBG
  57. ;
  58. ;=========================Mouse Functions================================
  59. ;Mouse movement is mapped to cursor keys: up,down,left,right.
  60. ;Right button  ==> Esc Key   (ctrl-[, 27)
  61. ;Left button   ==> Enter Key (ctrl-M, 13)
  62. ;Settings here are for Znix mouse with ballistics /m2 /fdefault.pro
  63. ;Other ballistics tried and all are acceptable: /m1, /m3, /m4.
  64. ;Also tested on Merit Mouse Z-1000 (like Znix /m4 no ballistics).
  65. ;========================================================================
  66. ;
  67. code segment
  68.         org 100h
  69.         assume cs:code,ds:code,es:code
  70. begin:  jmp start
  71.  
  72. leftkey                 equ  4b00h
  73. rightkey                equ  4d00h
  74. upkey                   equ  4800h
  75. downkey                 equ  5000h
  76. retkey                  equ  1c0dh
  77. esckey                  equ  011bh
  78.  
  79. mouse                   equ  51         ;interrupt 33h
  80. resetmousedriver        equ   0         ;reset mouse driver
  81. pressmouse              equ   5         ;mouse button press status
  82. mousemotion             equ  11         ;mouse cursor motion
  83. seteventhandler         equ  12         ;set mask and event handler
  84. setsensitivity          equ  15         ;set horiz & vert sensitivity
  85. setdoublespeed          equ  19         ;set double-speed threshold
  86. eventmask               equ  0000000000001011b
  87. ;evenmask bits 0,1,4: movement, left & right press
  88.  
  89. horizontalsensitivity   dw   8          ;8=default mickeys/8 pixels
  90. verticalsensitivity     dw  16          ;16=default mickeys/8 pixels
  91. ;
  92. ;Mouse driver resets to 8 and 16 respectively with function 00h.
  93. ;Znix and Merit both worked well with settings 8/16.
  94. ;
  95.  
  96. horizposition           dw   0          ;store mouse cursor position X
  97. vertposition            dw   0          ;store mouse cursor position Y
  98.  
  99. mouseeventhandler proc far
  100.         sti
  101.         push ax
  102.         push bx
  103.         push cx
  104.         push dx
  105.         push di
  106.         push ds                 ;ds=mouse driver data segment
  107.  
  108.         push cs
  109.         pop ds                  ;cs=ds=TSR data segment
  110.         cld
  111. ;
  112. ;Press Mouse supplies: BX=button status, CX=X mickeys, DX=Y mickeys
  113. ;                      The signed integers CX, DX measure UP==+, RIGHT==+
  114. ;                      Clears all button counts on each call.
  115. ;
  116.         mov ax,pressmouse       ;get status of button press
  117.         mov bx,0                ;for left button
  118.         int mouse
  119.         and ax,1                ;Press left button?
  120.         jz check_rightbutton    ;No? Then check right button.
  121.         mov cx,retkey           ;Yes. Then plug in RETURN key.
  122.         call stuffbuffer        ;near function call
  123. check_rightbutton:
  124.         mov ax,pressmouse       ;get status of button press
  125.         mov bx,2                ;for right button
  126.         int mouse
  127.         and ax,2                ;press right button?
  128.         jz checkcursor          ;No? Then check cursor keys.
  129.         mov cx,esckey           ;Yes. Then plug in ESC key.
  130.         call stuffbuffer        ;near function call
  131. checkcursor:
  132.         mov ax,mousemotion      ;Has the mouse cursor moved?
  133.         int mouse
  134.         mov ax,horizposition    ;save new horizontal position
  135.         add ax,cx               ;cx=X coord supplied by mouse driver
  136.         mov horizposition,ax
  137.         cmp ax,0                ;Has the mouse cursor moved?
  138.         je  motionvertical      ;No change, then check vertical
  139.         jg motionhorizontal     ;Make positive
  140.         not ax
  141. motionhorizontal:
  142.         mov bx,horizontalsensitivity
  143.         cmp ax,bx
  144.         jl motionvertical       ;didn't move enough for a change
  145.         cmp horizposition,0
  146.         mov cx,rightkey
  147.         jg  stuffit1            ;If positive, then stuff cursor Right
  148.         mov cx,leftkey
  149. stuffit1:
  150.         jmp stuffit2
  151. motionvertical:
  152.         mov ax,vertposition     ;save new vertical position
  153.         add ax,dx               ;dx=Y coord supplied by mouse driver
  154.         mov vertposition,ax
  155.         cmp ax,0
  156.         je return               ;no change, all done
  157.         jg testvertsens         ;If positive, then stuff cursor Up
  158.         not ax
  159. testvertsens:
  160.         mov bx,verticalsensitivity
  161.         cmp ax,bx
  162.         jl return               ;didn't move enough for a change
  163.         cmp vertposition,0
  164.         mov cx,downkey
  165.         jg stuffit2
  166.         mov cx,upkey
  167. stuffit2:
  168.         call manystuffbuffer    ;stuff key CX into keyboard buffer
  169.         mov vertposition,0      ;reset vertical saved position
  170.         mov horizposition,0     ;reset horizontal saved position
  171. return:
  172.         pop ds
  173.         pop di
  174.         pop dx
  175.         pop cx
  176.         pop bx
  177.         pop ax
  178.         ret
  179. mouseeventhandler endp
  180.  
  181. manystuffbuffer proc near
  182. ; AX=amount in mickeys mouse cursor has moved.
  183. ; BX=sensitivity in mickeys
  184. ; CX=key scan code to stuff into keyboard buffer
  185. manyloop:
  186.         call stuffbuffer
  187.         sub ax,bx
  188.         dec ax
  189.         cmp ax,bx
  190.         jge manyloop
  191.         ret
  192. manystuffbuffer endp
  193.  
  194. stuffbuffer proc near
  195.         cli                     ;Prevent interrupts from stuffing buffer
  196.         push es                 ;typeahead buffer start at *[0040:0080]
  197.         push si                 ;and end at *[0040:0082].
  198.         push di                 ;At segment 40h and offset 01ah is
  199.         push bx
  200.         mov di,40h              ;the offset into the typeahead buffer
  201.         mov es,di               ;for the character at the head. At
  202.         mov bx,es:[1ch]         ;segment 40h and offset 01ch is the
  203.         mov si,bx               ;offset into the typeahead buffer for
  204.         add bx,2                ;the last character. The buffer is full
  205.         cmp bx,es:[1ah]         ;if *[01ch]+2 == *[01ah].
  206.         je quit                 ;Full? Then don't add any more chars
  207.         cmp bx,es:[82h]         ;Offset less than end of buffer?
  208.         jl isroom               ;Yes. Then go ahead and stuff it.
  209.         mov bx,es:[80h]         ;Else wrap around to buffer start
  210. isroom:
  211.         mov es:[si],cx          ;CX=scan code to be stuffed
  212.         mov es:[1ch],bx         ;BX=new offset to last buffer character
  213. quit:   pop bx
  214.         pop di
  215.         pop si
  216.         pop es
  217.         sti                     ;let other routines stuff the buffer
  218.         ret                     ;near return
  219. stuffbuffer endp
  220.  
  221. endresident label byte
  222. ;
  223. ;All resident code is above this point.
  224. ;All code below this point gets trashed by TSR exit.
  225. ;For simplicity, no unload of 100h PSP area.
  226. ;
  227. start:
  228.         mov ax,resetmousedriver ;reset mouse driver to defaults
  229.         int mouse
  230.         cmp ax,0                ;bx=number of buttons, not used yet
  231.         je nomouse              ;exit if no mouse driver loaded
  232.         mov ax,cs               ;Hook mouse driver to event handler
  233.         mov es,ax               ;es=segment of event handler
  234.         mov dx,offset mouseeventhandler
  235.         mov ax,seteventhandler  ;mouse driver function code
  236.         mov cx,eventmask        ;signal mask for interrupt
  237.         int mouse
  238.         mov es,cs:[2ch]         ;es=environment segment
  239.         mov ah,49h              ;free DOS alloc memory
  240.         int 21h
  241.         lea dx,endresident      ;save resident code & exit.
  242.         int 27h                 ;outdated TSR method used for simplicity
  243. nomouse:
  244.         mov ax,0                ;Failure? Then do normal exit to dos.
  245.         int 21h                 ;don't bother with error message
  246. code    ends
  247.         end begin
  248.